package com.wwf.springbootrabbitmqordercancle.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfig {
    /**
     * 设置订单待支付队列，设置消息过期时间
     * @return
     */
    @Bean
    public Queue orderWillPayQueue(){
        /**设置消息过期时间**/
        HashMap<String, Object> props = new HashMap<>();
        //设置消息过期时间30秒
        props.put("x-message-ttl",30000);
        //设置消息过期时要转移的私信队列交换机
        props.put("x-dead-letter-exchange","orderNotPay.ex.dlx");
        //设置改队列关联转移的私信交换机路由key
        props.put("x-dead-letter-routing-key","orderNotPay.key.dlx");
        return new Queue("orderWillPay",true,false,false,props);
    }

    /**
     * 私信队列(订单超时未支付转移到的死信队列)
     * @return
     */
    @Bean
    public Queue dlxOrderNoPayCaneQueue(){
       return  new Queue("OrderNotPayCance");
    }

    /**
     * 订单待支付交换机
     * @return
     */
    @Bean
    Exchange orderWillPayExchange(){
        Exchange exchange = new DirectExchange("orderWillPay.ex");
        return exchange;
    }

    /**
     * 订单未支付死信交换机
     * @return
     */
    @Bean
    Exchange orderNotPayExchange(){
        DirectExchange directExchange = new DirectExchange("orderNotPay.ex.dlx");
        return directExchange;
    }

    /**
     * 绑定待支付交换机和待支付队列
     * @return
     */
    @Bean
    Binding orderWillPayBinding(){
        return BindingBuilder.bind(orderWillPayQueue()).to(orderWillPayExchange()).with("orderWillPay.key").noargs();
    }

    /**
     * 绑定超时未支付交换机和超时未支付队列（死信交换机和私信队列）
     * @return
     */
    @Bean
    Binding orderNotPayBinding(){
        return BindingBuilder.bind(dlxOrderNoPayCaneQueue()).to(orderNotPayExchange()).with("orderNotPay.key.dlx").noargs();
    }
}
